/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author dung.voanh
 */
public abstract class MyActor {
        
    protected int m_iX; // Position X display on Screen
    protected int m_iY; // Position Y display on Screen
    protected int m_iWidth;
    protected int m_iHeight;
    protected int m_iXMap; // Position X in map
    protected int m_iYMap; // Position Y in map    
    protected MySprite m_asprAction[];
    private int m_iCurrentAction = 0;    
    protected int m_state = DEFINE.ACTOR_STATE_NORMAL;
    protected boolean m_isDie;
    
    public MySprite GetCurrentSpriteAction(){
        return m_asprAction[m_iCurrentAction];
    }
    
    public int GetCurrentAction(){
        return m_iCurrentAction;
    }
    
    public int GetX(){
        return m_iX;
    }
    
    public int  GetY(){
        return m_iY;
    }
    
    public int GetCurrentFrame(){
        return m_asprAction[m_iCurrentAction].GetCurrentFrame();
    }
    
    public int GetState(){
        return m_state;
    }
    
    public void SetX(int x){
        m_iX = x;
        m_asprAction[m_iCurrentAction].SetPositionX(x);
    }
    
    public void setY(int y){
        m_iY = y;
        m_asprAction[m_iCurrentAction].SetPositionY(y);
    }
    
    public void SetPosition(int x, int y){
        m_asprAction[m_iCurrentAction].SetPosition(x, y);
    }
      
    public void SetXOnMap(int x){
        m_iXMap = x;
        int xMap = GameLayerManager.GetInstance().GetPositionX();
        SetX(m_iXMap - xMap);
    }
    
    public void setYOnMap(int y){
        m_iYMap = y;
        int yMap = GameLayerManager.GetInstance().GetPositionY();        
        setY(m_iYMap - yMap);
    }
    
    public void SetPositionOnMap(int x, int y){
        SetXOnMap(x);
        setYOnMap(y);
    }
    
      /**
     * @param detalX : move delta x direction
     * @param deltaY : move delta y direction
     */
    public void MoveOnMap(int detalX, int deltaY){
        m_iXMap += detalX;
        m_iYMap += deltaY;
        SetPositionOnMap(m_iXMap, m_iYMap);
    }
    
    public final void SetCurrentAction(int indexAction){
        if(indexAction >= 0 && indexAction < m_asprAction.length){
            m_iCurrentAction = indexAction;
            m_asprAction[m_iCurrentAction].SetPosition(m_iX, m_iY);
        }            
    }

    public final void SetCurrentAction(int indexAction, int indexFrame){
        SetCurrentAction(indexAction);
        m_asprAction[m_iCurrentAction].SetCurrentFrame(indexFrame);
    }
    
    public void SetCurrentFrame(int indexFrame){
        m_asprAction[m_iCurrentAction].SetCurrentFrame(indexFrame);
    }

    public boolean ISDie(){
        return m_isDie;
    }
    
    public void Die(){
        m_isDie = true;
        //System.out.println("Die: this = " + this.toString());
    }
    
    public MyActor(int aiSpriteID[]) {
        m_asprAction = new MySprite[aiSpriteID.length];
        for(int i = 0; i < aiSpriteID.length; i++){
            m_asprAction[i] = ResourceManager.GetInstance().GetSprite(aiSpriteID[i]);
        }
        m_iWidth = m_asprAction[0].m_iWidth;
        m_iHeight = m_asprAction[0].m_iHeight;
    }

    public MyActor(String astrSpriteName[]) {
        m_asprAction = new MySprite[astrSpriteName.length];
        for(int i = 0; i < astrSpriteName.length; i++){
            m_asprAction[i] = ResourceManager.GetInstance().GetSprite(astrSpriteName[i]);
        }
        m_iWidth = m_asprAction[0].m_iWidth;
        m_iHeight = m_asprAction[0].m_iHeight;
    }
    
    public void SetState(int state){
        m_state = state;
    }
    
    public void Update(){
        SetPositionOnMap(m_iXMap, m_iYMap);
    }
            
    public void Render(Graphics g){
        if(m_isDie == false){
            m_asprAction[m_iCurrentAction].Render(g);
        }
    }
    
    public boolean Collision(MyActor other){
        int wLeft = (this.m_iXMap > other.m_iXMap) ? this.m_iXMap : other.m_iXMap ;
        int wTop = (this.m_iYMap > other.m_iYMap) ? this.m_iYMap : other.m_iYMap;
        int wRight = (this.m_iXMap + this.m_iWidth);
        if(wRight > other.m_iXMap + other.m_iWidth){
            wRight = other.m_iXMap + other.m_iWidth;
        }
        
        int minHeight = (this.m_iHeight < other.m_iHeight) ? this.m_iHeight : other.m_iHeight;
        int minWidth = (this.m_iWidth < other.m_iWidth) ? this.m_iWidth : other.m_iWidth;
        
        int wBottom = this.m_iYMap + this.m_iHeight;
        if(wBottom > other.m_iYMap + other.m_iHeight){
            wBottom = other.m_iYMap + other.m_iHeight;
        }
        
        if(wRight - wLeft > minWidth/2 && wBottom - wTop > minHeight/2){
            return true;
        }
        return false;
    }
    
//    public boolean Collision(MyActor other){
//        int wLeft = (this.m_iXMap > other.m_iXMap) ? this.m_iXMap : other.m_iXMap ;
//        int wTop = (this.m_iYMap > other.m_iYMap) ? this.m_iYMap : other.m_iYMap;
//        int wRight = (this.m_iXMap + this.m_iWidth);
//        if(wRight > other.m_iXMap + other.m_iWidth){
//            wRight = other.m_iXMap + other.m_iWidth;
//        }
//        
//        int wBottom = this.m_iYMap + this.m_iHeight;
//        if(wBottom > other.m_iYMap + other.m_iHeight){
//            wBottom = other.m_iYMap + other.m_iHeight;
//        }
//        
//        if(wLeft <= wRight && wTop <= wBottom){
//            return true;
//        }
//        return false;
//    }
}
